<template>
  <div>
    <h1>文章管理</h1>
    <el-table :data="item">
      <el-table-column prop="_id" label="ID" width="230"> </el-table-column>
      <el-table-column prop="parentname" label="所属分类" width="230"> </el-table-column>
      <el-table-column prop="name" label="文章标题"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            @click="$router.push(`/articles/edit/${scope.row._id}`)"
            size="small"
            >编辑</el-button
          >
          <el-button @click="remove(scope.row)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "ArticlesList",

  data() {  
    return {
      item: [],
      parentname:{},
      num:0
    };
  },
  methods: {
    // // 定义一个函数,实现标题的拼接>>能把所属标题给展示出来，大致效果是把item对应的子数据中的parentname拼接成一个新字符串

    async fetch() {
      const res =await  this.$http1.get("rest/articles");

      //在数据传过来的时候进行操作即可 给res.data添加一个parentsname属性就得了
      // 给数组添加对象的方法 this.item=[...1,...2]
      for(var x of res.data)
      {
        
        if(x.parent!=null)
        {
          x.parentname=""
          for(var y of x.parent)
          {  
            this.num++
            x.parentname+=y.name+" "
          }
        }
        
      }
      this.item=res.data

    },
    async remove(row) {
      this.$confirm(`此操作将永久删除文章 ${row.name}, 是否继续?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http1.delete(`rest/articles/${row._id}`);
          this.$message({

            type: "success",
            message: "删除成功!",
          });
          this.fetch();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.fetch();
    // this.getparentname()
  },

};
</script>

<style>
</style>